/* rqmrw.p    - UPDATE MRP FOR REQUISITION                                    */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                        */
/* All rights reserved worldwide.  This is an unpublished work.               */
/*V8:ConvertMode=NoConvert                                                    */

/* Revision: 8.6      LAST MODIFIED: 04/22/97   By: B. Gates          *J1Q2*  */
/* REVISION: 8.6E     LAST MODIFIED: 02/23/98   BY: *L007* A. Rahane          */
/* REVISION: 8.6E     LAST MODIFIED: 10/04/98   BY: *J314* Alfred Tan         */
/* REVISION: 8.6E     LAST MODIFIED: 09/09/99   BY: *J39R* Reetu Kapoor       */
/* REVISION: 9.1      LAST MODIFIED: 08/23/00   BY: *N0ND* Mark Brown         */
/* REVISION: 9.1      LAST MODIFIED: 11/08/00   BY: *N0TN* Jean Miller        */

/*!UPDATES THE MRP DETAIL FILE, FOR DIRECT REQ'S ONLY*/
         {mfdeclre.i}
/*N0TN*/ {gplabel.i}

/* ********** Begin Translatable Strings Definitions ********* */

/*N0TN* &SCOPED-DEFINE rqmrw_p_1 "PURCHASE REQUISITION" */
/* MaxLen: Comment: */

/* ********** End Translatable Strings Definitions ********* */


         define input parameter p_first_call as logical no-undo.
         define input parameter p_req_site like pod_site no-undo.
         define input parameter p_req_nbr like rqpo_req_nbr no-undo.
         define input parameter p_req_line like rqpo_req_line no-undo.

         define variable old_db as character no-undo.
         define variable err-flag as integer no-undo.
         define variable qty_open like rqd_req_qty no-undo.
         define variable requm like rqd_um no-undo.
         define variable conversion_factor as dec no-undo.

         if p_first_call then do:
            /*FIRST TIME IN, WE SWITCH DB'S IF WE HAVE TO THEN
              CALL OURSELVES AGAIN, WHICH WILL EXECUTE THE ELSE BLOCK*/
            find si_mstr where si_site = p_req_site no-lock.

            if si_db <> global_db then do:
               old_db = global_db.
               {gprun.i ""gpalias3.p"" "(si_db, output err-flag)"}
            end.

            {gprun.i ""rqmrw.p""
               "(input false, input p_req_site, input p_req_nbr,
                 input p_req_line)"}

            /*SWITCH BACK TO CENTRAL PO DB IF WE HAVE TO*/
            if old_db <> global_db then do:
               {gprun.i ""gpalias3.p"" "(old_db, output err-flag)"}
            end.
         end.
         else do:
            find rqm_mstr where rqm_nbr = p_req_nbr no-lock.

            find rqd_det where rqd_nbr = p_req_nbr and rqd_line = p_req_line
            no-lock.

            /*REMOVE ANY MRP DETAIL RECORDS*/
            for each mrp_det exclusive-lock
            where mrp_dataset = "req_det"
            and mrp_nbr = rqd_nbr
            and mrp_line = string(rqd_line):
               {inmrp.i &part=mrp_det.mrp_part &site=mrp_det.mrp_site}
               delete mrp_det.
            end.

            /*HAS TO BE OPEN AND NOT A MEMO*/
/*J39R*    if rqd_status = "x" or rqd_type = "m" then leave.  */
/*J39R*/   if rqd_status = "c" or
/*J39R*/      rqd_status = "x" or
/*J39R*/      rqd_type   = "m" then
/*J39R*/      leave.


            /*HAS TO BE A VALID ITEM*/
            find pt_mstr where pt_part = rqd_part no-lock no-error.
            if not available pt_mstr then leave.


            /*GET REQ LINE OPEN QTY*/
            {gprun.i ""rqoqty.p""
            "(input true, input p_req_site, input p_req_nbr, input p_req_line,
              output qty_open, output requm)"}


            /*GET UM CONVERSION FACTOR*/
            conversion_factor = 1.

            if rqd_um <> pt_um then do:
               {gprun.i ""gpumcnv.p""
                  "(input rqd_um, input pt_um, input pt_part,
                    output conversion_factor)"}
            end.

            qty_open = qty_open * conversion_factor.


            /*NOW WE CAN TELL MRP...*/
/*N0TN*/    /* Changed pre-processor to Term */
            {mfmrw.i
               req_det
               rqd_part
               rqd_nbr
               string(rqd_line)
               """"
               ?
               rqd_need_date
               qty_open
               "SUPPLYF"
               PURCHASE_REQUISITION
               rqd_site}
            end.
